(function(){
	var webrtc = {
		socket:null,
		events:{},
		init:function(ip,name){
			this.socket = io(ip)
			this.allEvents()
			this.join(name)
		},
		join:function(name){
			this.socket.emit('join',name)
		},
		call:function(self,names){
			this.socket.emit('call',{caller:self,all:JSON.stringify(names)})
		},
		uncall:function(self,names){
					this.socket.emit('uncall',{caller:self,all:JSON.stringify(names)})
				},
		acceptCall:function(data){
			this.socket.emit('accept',data)
		},
		refuseCall:function(data){
			this.socket.emit('refuse',data)
		},
		on:function(events){
			this.events = events
		},
		emit:function(ev,data){
			this.events[ev](data)
		},
		allEvents:function(){
			var _this = this
			this.socket.on('subcall',function(data){
				_this.emit('listenCall',data)
			})
			this.socket.on('fail',function(data){
					_this.emit('notOnline',data)
				})
			this.socket.on('anserRefuse',function(data){
				    _this.emit('listenRefuse',data.accepter)
				})
			this.socket.on('failRoom',function(val){
					_this.emit('alreadyRoom',val)
				})
			this.socket.on('showvd',function(data){
//                  window.location = 'https://192.168.43.89:8443/rtc.html?id='+data.roomNum  
                    _this.emit('allAccept',data)
				})
				this.socket.on('listenUncall',function(data){
				//                  window.location = 'https://192.168.43.89:8443/rtc.html?id='+data.roomNum  
				                    _this.emit('listenUncall',data)
								})	
								this.socket.on('connect', function(data){
     _this.emit('connect_timeout',data)
});
this.socket.on('readyCall', function(data){
     _this.emit('readyCall',data)
});

		}			
	}
	this.webrtc = webrtc
})()

            
            var con = prompt('输入名字')
			var olis = document.getElementsByTagName('li')
			webrtc.init('https://192.168.0.120:8443',con)
			function call(){
				var names = []
			  for(var i=0;i<olis.length;i++){
			  	if(olis[i].children[0].checked==true){
			  		names.push(olis[i].innerText) 
			  	}
			  }
			  webrtc.call(con,names)
			}
			function uncall(){
				var names = []
				for(var i=0;i<olis.length;i++){
					if(olis[i].children[0].checked==true){
						names.push(olis[i].innerText) 
					}
				}
				webrtc.uncall(con,names) //多人拨打，传入当前用户的唯一标识，以及其他用户的唯一标识
			}
			webrtc.on({
				listenCall:function(data){
					var co = confirm('是否接听？')
					co ? webrtc.acceptCall(data) : webrtc.refuseCall(data)
				},
				notOnline:function(data){
					alert(data+'不在线')
				},
				listenUncall:function(data){
					alert(data+'放弃了拨打')
				},
				connect_timeout:function(data){
					console.log(data+'超时')
				},
				alreadyRoom:function(data){
					alert(data+'已在房间')
				},
				listenRefuse:function(data){//被拨打者拒绝事件
					alert(data+'拒绝了你')
				},
				allAccept:function(data){
console.log(data)
					window.location = 'https://192.168.0.120:8443/rtc.html?id='+data.roomNum
				},
				readyCall:function(data){
					alert(data+'正在被拨打')
				}	
				})
				
			
			